package constant

import (
	"github.com/lab-online/internal/shared/code"
	"github.com/lab-online/pkg/errors"
	"github.com/lab-online/pkg/gen"
	"github.com/lab-online/pkg/resp"
)

const (
	dbUnknownError       = code.DatabaseUnknownError
	dbCourseNotFound     = code.DatabaseCourseNotFound
	dbExperimentNotFound = code.DatabaseExperimentNotFound
)

const (
	baseReqErrorCode         = resp.CodeBadRequest + domainCode
	codeCourseNotFound       = baseReqErrorCode + 1
	codeExperimentNotFound   = baseReqErrorCode + 2
	codeExperimentQueueExist = baseReqErrorCode + 3
)

var (
	RESTErrCourseNotFound       = errors.NewRESTError(400, codeCourseNotFound, "未找到指定课程")
	RESTErrExperimentNotFound   = errors.NewRESTError(400, codeExperimentNotFound, "未找到指定实验")
	RESTErrExperimentQueueExist = errors.NewRESTError(400, codeExperimentQueueExist, "实验队列已存在")
	DBErrUnknown                = errors.NewDBError(dbUnknownError, "未定义错误")
	DBErrCourseNotFound         = errors.NewDBError(dbCourseNotFound, "未找到指定课程")
	DBErrExperimentNotFound     = errors.NewDBError(dbExperimentNotFound, "未找到指定实验")
)

func ErrCodeMap() gen.CodeMap {
	return gen.ParseCodeMapFromErrors(gen.ErrorMap{
		"RemoveCourse": {
			DBErrCourseNotFound,
			DBErrUnknown,
		},
		"UpdateCourse": {
			DBErrCourseNotFound,
			DBErrUnknown,
		},
		"SearchCourse": {
			RESTErrCourseNotFound,
		},
		"GetCourses": {
			DBErrUnknown,
		},
		"GetCourse": {
			DBErrUnknown,
			RESTErrCourseNotFound,
		},

		"RemoveExperiment": {
			DBErrExperimentNotFound,
			DBErrUnknown,
		},
		"UpdateExperiment": {
			DBErrExperimentNotFound,
			DBErrUnknown,
		},
		"SearchExperiment": {
			RESTErrExperimentNotFound,
		},
		"GetExperiments": {
			DBErrUnknown,
			RESTErrExperimentNotFound,
		},
	})
}
